package com.bwda.dsrs.filecenter.controller;

import com.bwda.dsrs.base.util.CopyUtil;
import com.bwda.dsrs.filecenter.annotations.NeedRole;
import com.bwda.dsrs.filecenter.aspect.LogTypeEnum;
import com.bwda.dsrs.filecenter.aspect.Loggable;
import com.bwda.dsrs.filecenter.domain.BusinessConstants;
import com.bwda.dsrs.filecenter.domain.condition.sensitivelevel.SensitiveLevelCondition;
import com.bwda.dsrs.filecenter.domain.condition.sensitiverule.SensitiveRuleCondition;
import com.bwda.dsrs.filecenter.domain.condition.sensitivewords.SensitiveWordsCondition;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.filecenter.domain.form.sensitivelevel.SensitiveLevelQueryForm;
import com.bwda.dsrs.filecenter.domain.form.sensitivelevel.SensitiveLevelUpdateForm;
import com.bwda.dsrs.filecenter.domain.form.sensitiverule.SensitiveRuleCreateForm;
import com.bwda.dsrs.filecenter.domain.form.sensitivewords.SensitiveWordsQueryForm;
import com.bwda.dsrs.filecenter.domain.po.sensitivelevel.SensitiveLevelPo;
import com.bwda.dsrs.filecenter.domain.po.sensitiverule.SensitiveRulePo;
import com.bwda.dsrs.filecenter.domain.po.sensitivewords.SensitiveWordsPo;
import com.bwda.dsrs.filecenter.domain.vo.sensitivelevel.SensitiveLevelVo;
import com.bwda.dsrs.filecenter.domain.vo.sensitivewords.SensitiveWordsVo;
import com.bwda.dsrs.filecenter.service.SensitiveLevelService;
import com.bwda.dsrs.filecenter.service.SensitiveRuleService;
import com.bwda.dsrs.filecenter.service.SensitiveWordsService;
import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 业务配置--敏感等级--控制类
 *
 * @author zhangyinmei
 * @date 2019年4月28日
 */
@RestController
@RequestMapping("/b/sensitivelevel")
@Api(value = "/b/sensitivelevel", tags = "敏感等级")
public class SensitiveLevelController extends BaseController {
	private static final Logger logger = LoggerFactory.getLogger(SensitiveLevelController.class);

	@Autowired
	private SensitiveLevelService sensitiveLevelService;

	@Autowired
	private SensitiveRuleService sensitiveRuleService;

	@Autowired
	private SensitiveWordsService sensitiveWordsService;

	/**
	 * 查询敏感字段列表
	 *
	 * @param form
	 * @return
	 * @throws BusinessServiceException
	 */
	@Loggable(descp = "查询敏感字段列表",type = LogTypeEnum.QUERY,include = "")
	@ApiOperation(value = "查询敏感字段列表",notes = "查询敏感字段列表",httpMethod = "GET")
	@RequestMapping(value = "/querySensitiveWordsList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<SensitiveWordsVo>> querySensitiveWordsList(@ModelAttribute@Valid SensitiveWordsQueryForm form){
		logger.info("查询敏感字段列表");
		SensitiveWordsCondition condition = this.getConditionByQueryFormToWords(form);
		condition.setStatus(BusinessConstants.NumberConstant.ZERO);
		List<SensitiveWordsPo> poList = sensitiveWordsService.queryList(condition);
		List<SensitiveWordsVo> voList = CopyUtil.transfer(poList, SensitiveWordsVo.class);
		return getSuccessResult(getListResponse(voList));
	}

	/**
	 * 查询敏感等级列表(带分页)
	 *
	 * @param form
	 * @return
	 * @throws BusinessServiceException
	 */
	@Loggable(descp = "查询敏感等级列表",type = LogTypeEnum.QUERY,include = "")
	@ApiOperation(value = "查询敏感等级列表(带分页)",notes = "查询敏感等级列表(带分页)",httpMethod = "GET")
	@RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreCutPageResponse<SensitiveLevelVo>> queryPageList(@ModelAttribute@Valid SensitiveLevelQueryForm form) throws BusinessServiceException {
		logger.info("查询敏感等级列表(带分页)");
		SensitiveLevelCondition condition = this.getConditionByQueryForm(form);
		List<SensitiveLevelVo> voList = new ArrayList<>();
		int count = sensitiveLevelService.queryCount(condition);
		if (count > BusinessConstants.NumberConstant.ZERO) {
			List<SensitiveLevelPo> poList = sensitiveLevelService.queryList(condition);
			voList = CopyUtil.transfer(poList, SensitiveLevelVo.class);
			//创建“敏感字段规则”查询参数实体类
			SensitiveRuleCondition sensitiveRuleCondition;
			List<SensitiveRulePo> sensitiveRulePoList;
			//查询敏感字段信息
			SensitiveWordsPo sensitiveWordsPo;
			for(SensitiveLevelVo sensitiveLevelVo : voList){
				//根据敏感等级ID查询敏感等级规则列表
				sensitiveRuleCondition = new SensitiveRuleCondition();
				//设置敏感等级ID
				sensitiveRuleCondition.setSensitiveId(sensitiveLevelVo.getId());
				sensitiveRulePoList = sensitiveRuleService.queryList(sensitiveRuleCondition);
				//遍历敏感等级规则集合，根据敏感字段ID获取敏感字段信息
				for(SensitiveRulePo sensitiveRulePo : sensitiveRulePoList){
					//查询敏感字段信息
					sensitiveWordsPo = sensitiveWordsService.query(sensitiveRulePo.getWordId());
					if(sensitiveWordsPo == null){
						continue;
					}
					sensitiveRulePo.setSensitiveWordName(sensitiveWordsPo.getName());
				}
				sensitiveLevelVo.setSensitiveRulePoList(sensitiveRulePoList);
			}
		}
		return getSuccessResult(getPageResponse(form, count, voList));
	}

	/**
	 * 校验敏感等级
	 *
	 * @param form
	 * @return
	 * @throws BusinessServiceException
	 */
	@Loggable(descp = "校验敏感等级",type = LogTypeEnum.QUERY,include = "")
    @ApiOperation(value = "校验敏感等级",notes = "校验敏感等级",httpMethod = "GET")
    @RequestMapping(value = "/checkSensitiveLeveRule", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<SensitiveLevelVo> checkSensitiveLevelRule(@ModelAttribute@Valid SensitiveLevelUpdateForm form) throws BusinessServiceException {
		logger.info("校验敏感等级");
		ResponseEntity responseEntity = sensitiveLevelService.checkSensitiveLevelRule(form);
        return responseEntity;
    }

	/**
	 * 修改敏感等级
	 *
	 * @param form
	 * @return
	 * @throws BusinessServiceException
	 */
	@NeedRole("sysadminrole")
	@Loggable(descp = "修改敏感等级",type = LogTypeEnum.UPDATE,include = "")
	@ApiOperation(value = "修改敏感等级",notes = "修改敏感等级",httpMethod = "POST")
	@RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity update(@ModelAttribute@Valid SensitiveLevelUpdateForm form) throws BusinessServiceException {
		if(form.getId() == null){
			return getFailResult("敏感等级不存在");
		}
		if(form.getSensitiveState()==null){
			return getFailResult("请指定敏感等级是否启用");
		}
		SensitiveLevelCondition condition = new SensitiveLevelCondition();
		condition.setDelFlag(BusinessConstants.NumberConstant.ZERO);
		condition.setSensitiveState(BusinessConstants.NumberConstant.ZERO);
		int count = sensitiveLevelService.queryCount(condition);
		if(form.getId()!=count&&form.getSensitiveState().equals(BusinessConstants.INTEGER_ONE)){
			return getFailResult("请从最高敏感等级开始设置禁用");
		}
		logger.info("修改敏感等级");
		SensitiveLevelPo po = CopyUtil.transfer(form, SensitiveLevelPo.class);
		//设置更新时间
		po.setUpdateTime(new Date());
		sensitiveLevelService.update(po);
		SensitiveRuleCondition sensitiveRuleCondition = new SensitiveRuleCondition();
		sensitiveRuleCondition.setSensitiveId(form.getId());
		List<SensitiveRulePo> sensitiveRulePoList = sensitiveRuleService.queryList(sensitiveRuleCondition);
		for(SensitiveRulePo p : sensitiveRulePoList){
			//先删除
			sensitiveRuleService.delete(p.getId());
		}
		//再新增
		List<SensitiveRuleCreateForm> sensitiveRuleStrList = form.getSensitiveRuleCreateFormList();
		if(sensitiveRuleStrList != null && !sensitiveRuleStrList.isEmpty()){
			SensitiveRulePo sensitiveRulePo;
			for(SensitiveRuleCreateForm sensitiveRuleCreateForm : sensitiveRuleStrList){
				sensitiveRulePo = CopyUtil.transfer(sensitiveRuleCreateForm, SensitiveRulePo.class);
				sensitiveRulePo.setSensitiveId(po.getId());
				sensitiveRuleService.insert(sensitiveRulePo);
			}
		}
		return getSuccessResult();
	}

	/**
	 * SensitiveLevelQueryForm转换为SensitiveLevelCondition
	 *
	 * @param form
	 * @return
	 */
	private SensitiveLevelCondition getConditionByQueryForm(SensitiveLevelQueryForm form) {
		SensitiveLevelCondition condition = CopyUtil.transfer(form, SensitiveLevelCondition.class);
		return condition;
	}

	/**
	 * SensitiveWordsQueryForm转换为SensitiveWordsCondition
	 *
	 * @param form
	 * @return
	 */
	private SensitiveWordsCondition getConditionByQueryFormToWords(SensitiveWordsQueryForm form) {
		SensitiveWordsCondition condition = CopyUtil.transfer(form, SensitiveWordsCondition.class);
		return condition;
	}
}